@@ -4,6 +4,15 @@ class @AgentEditPage |
||
4 | 4 |
@showCorrectRegionsOnStartup() |
5 | 5 |
$("form.agent-form").on "submit", => @updateFromEditors() |
6 | 6 |
|
7 |
+ # Validate agents_options Json on form submit |
|
8 |
+ $('form.agent-form').submit (e) -> |
|
9 |
+ if $('textarea#agent_options').length |
|
10 |
+ try |
|
11 |
+ JSON.parse $('#agent_options').val() |
|
12 |
+ catch err |
|
13 |
+ e.preventDefault() |
|
14 |
+ alert 'Sorry, there appears to be an error in your JSON input. Please fix it before continuing.' |
|
15 |
+ |
|
7 | 16 |
$("#agent_name").each -> |
8 | 17 |
# Select the number suffix if this is a cloned agent. |
9 | 18 |
if matches = this.value.match(/ \(\d+\)$/) |
@@ -14,6 +14,7 @@ Capybara.default_max_wait_time = CAPYBARA_TIMEOUT |
||
14 | 14 |
|
15 | 15 |
RSpec.configure do |config| |
16 | 16 |
config.include Warden::Test::Helpers |
17 |
+ config.include AlertConfirmer, type: :feature |
|
17 | 18 |
config.before :suite do |
18 | 19 |
Warden.test_mode! |
19 | 20 |
end |
@@ -13,4 +13,21 @@ describe "Creating a new agent", js: true do |
||
13 | 13 |
|
14 | 14 |
expect(page).to have_text("Test Trigger Agent") |
15 | 15 |
end |
16 |
+ |
|
17 |
+ it "creates an alert if a new agent with invalid json is submitted" do |
|
18 |
+ login_as(users(:bob)) |
|
19 |
+ visit "/" |
|
20 |
+ page.find("a", text: "Agents").trigger(:mouseover) |
|
21 |
+ click_on("New Agent") |
|
22 |
+ |
|
23 |
+ select2("Trigger Agent", from: "Type") |
|
24 |
+ fill_in(:agent_name, with: "Test Trigger Agent") |
|
25 |
+ click_on("Toggle View") |
|
26 |
+ |
|
27 |
+ fill_in(:agent_options, with: '{ |
|
28 |
+ "expected_receive_period_in_days": "2" |
|
29 |
+ "keep_event": "false" |
|
30 |
+ }') |
|
31 |
+ expect(get_alert_text_from { click_on "Save" }).to have_text("Sorry, there appears to be an error in your JSON input. Please fix it before continuing.") |
|
32 |
+ end |
|
16 | 33 |
end |
@@ -0,0 +1,15 @@ |
||
1 |
+require 'capybara_helper' |
|
2 |
+ |
|
3 |
+describe "Editing an agent", js: true do |
|
4 |
+ it "creates an alert if a agent with invalid json is submitted" do |
|
5 |
+ login_as(users(:bob)) |
|
6 |
+ visit("/agents/#{agents(:bob_website_agent).id}/edit") |
|
7 |
+ click_on("Toggle View") |
|
8 |
+ |
|
9 |
+ fill_in(:agent_options, with: '{ |
|
10 |
+ "expected_receive_period_in_days": "2" |
|
11 |
+ "keep_event": "false" |
|
12 |
+ }') |
|
13 |
+ expect(get_alert_text_from { click_on "Save" }).to have_text("Sorry, there appears to be an error in your JSON input. Please fix it before continuing.") |
|
14 |
+ end |
|
15 |
+end |
@@ -0,0 +1,53 @@ |
||
1 |
+module AlertConfirmer |
|
2 |
+ def reject_confirm_from &block |
|
3 |
+ handle_js_modal 'confirm', false, &block |
|
4 |
+ end |
|
5 |
+ |
|
6 |
+ def accept_confirm_from &block |
|
7 |
+ handle_js_modal 'confirm', true, &block |
|
8 |
+ end |
|
9 |
+ |
|
10 |
+ def accept_alert_from &block |
|
11 |
+ handle_js_modal 'alert', true, &block |
|
12 |
+ end |
|
13 |
+ |
|
14 |
+ def get_alert_text_from &block |
|
15 |
+ handle_js_modal 'alert', true, true, &block |
|
16 |
+ get_modal_text 'alert' |
|
17 |
+ end |
|
18 |
+ |
|
19 |
+ def get_modal_text(name) |
|
20 |
+ page.evaluate_script "window.#{name}Msg;" |
|
21 |
+ end |
|
22 |
+ |
|
23 |
+ private |
|
24 |
+ |
|
25 |
+ def handle_js_modal name, return_val, wait_for_call = false, &block |
|
26 |
+ modal_called = "window.#{name}.called" |
|
27 |
+ page.execute_script " |
|
28 |
+ window.original_#{name}_function = window.#{name}; |
|
29 |
+ window.#{name} = function(msg) { window.#{name}Msg = msg; window.#{name}.called = true; return #{!!return_val}; }; |
|
30 |
+ #{modal_called} = false; |
|
31 |
+ window.#{name}Msg = null;" |
|
32 |
+ |
|
33 |
+ block.call |
|
34 |
+ |
|
35 |
+ if wait_for_call |
|
36 |
+ timed_out = false |
|
37 |
+ timeout_after = Time.now + Capybara.default_max_wait_time |
|
38 |
+ loop do |
|
39 |
+ if page.evaluate_script(modal_called).nil? |
|
40 |
+ raise 'appears that page has changed since this method has been called, please assert on page before calling this' |
|
41 |
+ end |
|
42 |
+ |
|
43 |
+ break if page.evaluate_script(modal_called) || |
|
44 |
+ (timed_out = Time.now > timeout_after) |
|
45 |
+ |
|
46 |
+ sleep 0.001 |
|
47 |
+ end |
|
48 |
+ raise "#{name} should have been called" if timed_out |
|
49 |
+ end |
|
50 |
+ ensure |
|
51 |
+ page.execute_script "window.#{name} = window.original_#{name}_function" |
|
52 |
+ end |
|
53 |
+end |